package cn.admobiletop.adsuyi.adapter.toutiao.listener;

import android.os.Handler;
import android.os.Looper;

import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;

import cn.admobiletop.adsuyi.ad.listener.ADSuyiRewardVodAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.data.RewardVodAdInfo;
import cn.admobiletop.adsuyi.config.ADSuyiErrorConfig;

/**
 * @author ciba
 * @description 激励视频广告监听
 * @date 2020/4/2
 */
public class RewardVodAdListener extends BaseAdListener<ADSuyiRewardVodAdListener> implements TTAdNative.RewardVideoAdListener {
    private RewardVodAdInfo rewardVodAdInfo;
    private Handler handler = new Handler(Looper.getMainLooper());

    public RewardVodAdListener(String platformPosId, ADSuyiRewardVodAdListener adListener) {
        super(platformPosId, adListener);
    }

    @Override
    public void onError(final int code, final String message) {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    onAdFailed(code, message);
                }
            });
        }
    }

    @Override
    public void onRewardVideoAdLoad(final TTRewardVideoAd ttRewardVideoAd) {
        if (ttRewardVideoAd == null) {
            if (handler != null) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onAdFailed(ADSuyiErrorConfig.AD_FAILED_AD_IS_EMPTY, ADSuyiErrorConfig.MSG_AD_FAILED_AD_IS_EMPTY);
                    }
                });
            }
            return;
        }
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null) {
                        rewardVodAdInfo = new RewardVodAdInfo(getPlatformPosId());
                        rewardVodAdInfo.setAdapterAdInfo(ttRewardVideoAd);
                        rewardVodAdInfo.setAdListener(getAdListener());

                        getAdListener().onAdReceive(rewardVodAdInfo);
                    }
                }
            });
        }
    }

    @Override
    public void onRewardVideoCached() {
    }

    @Override
    public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null && rewardVodAdInfo != null) {
                        getAdListener().onVideoCache(rewardVodAdInfo);
                    }
                }
            });
        }
    }

    /**
     * 广告是否资源
     */
    @Override
    public void release() {
        super.release();
        if (rewardVodAdInfo != null) {
            rewardVodAdInfo.release();
            rewardVodAdInfo = null;
        }
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }
}
